package com.hong.cloud.mall.practice.cartorder.util;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * 描述：实现生产者消费者模式
 */

public class MyBlockingQueue {
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(10);

        Runnable producer = () -> {
            while (true) {
                try {
                    queue.put("product");
                    System.out.println("生产者生产了一个产品，当前队列长度为：" + queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(producer).start();

        Runnable consumer = () -> {
            while (true) {
                try {
                    queue.take();
                    System.out.println("消费者消费了一个产品，当前队列长度为：" + queue.size());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(consumer).start();
    }
}
